package com.chengzhi.disruptor.quickstart;

import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;

public class OrderEventProducer {
    private RingBuffer<OrderEvent> ringBuffer;

    public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void send(ByteBuffer buffer) {
        //获取可用序号
        long sequence = ringBuffer.next();
        try {
            //获取空对象
            OrderEvent orderEvent = ringBuffer.get(sequence);
            //赋值
            orderEvent.setOrderId(buffer.getLong(0));
        } finally {
            ringBuffer.publish(sequence);
        }
    }

}
